var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var timeStorage;
(function (timeStorage) {
    timeStorage["first"] = "_result_gc_first";
    timeStorage["sec"] = "_result_gc_second";
})(timeStorage || (timeStorage = {}));
var timeCount;
(function (timeCount) {
    timeCount[timeCount["first"] = 10] = "first";
    timeCount[timeCount["sec"] = 15] = "sec";
})(timeCount || (timeCount = {}));
var Lottery = /** @class */ (function () {
    function Lottery(times) {
        this.GIFTLIST = [
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            },
            {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }, {
                name: '1',
                type: 1,
                word: 'hahaha',
                message: 'nihao'
            }
        ];
        this.typeMap = ['无', '立即', '当天', '预支'];
        this.CARD_COUNT = this.GIFTLIST.length;
        this.GET_COUNT = 10;
        this.CARD = {
            width: 75,
            height: 100
        };
        this.BOX = {
            width: 300,
            height: 200
        };
        this.timerList = {};
        this.timerFastList = {};
        this.result = [];
        this.idMapResult = {};
        this.times = times;
        this.timeStorageName = timeStorage[this.times];
        this.GET_COUNT = timeCount[this.times];
        this.GIFTLIST = [];
        for (var i = 0; i < 30; i++) {
            this.GIFTLIST.push({
                name: '名字' + i,
                type: 1,
                word: '显示点什么吧，比如：' + i * 2342,
                message: '可有可无'
            });
        }
    }
    Lottery.prototype.initCard = function () {
        return __awaiter(this, void 0, void 0, function () {
            var location, i, element;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        location = this.randomLocation();
                        this.card_location_list = location;
                        for (i = 0; i < location.length; i++) {
                            element = location[i];
                            this.drawCard(element.x, element.y, element.id);
                        }
                        this.BOX_LOCATION = this._getBoxLocation();
                        console.log(this.BOX_LOCATION.y);
                        return [4 /*yield*/, this._delay(function () {
                                for (var i = 0; i < location.length; i++) {
                                    var element = location[i];
                                    _this.move(element, 10);
                                }
                                setTimeout(function () { _this.addEventOnBox(); }, 2200);
                            }, 800)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    Lottery.prototype.randomLocation = function () {
        var location = [];
        var count = this.GET_COUNT;
        var width = document.body.offsetWidth - this.CARD.width;
        var height = document.body.offsetHeight - 250 - this.CARD.height;
        var randomWidthList = this.randomPush(width, count);
        var randomHeightList = this.randomPush(height, count);
        for (var i = 0; i < count; i++) {
            location.push({
                x: randomWidthList[i],
                y: randomHeightList[i],
                id: Date.now().toString() + i
            });
        }
        return location;
    };
    Lottery.prototype.drawCard = function (left, top, id) {
        if (id === void 0) { id = Date.now().toString(); }
        var div = document.createElement('div');
        div.setAttribute('style', "width:" + (this.CARD.width.toString() + 'px') + ";height:" + (this.CARD.height.toString() + 'px') + ";left: " + (left + 'px') + ";top:" + (top + 'px'));
        div.setAttribute('class', 'card');
        div.setAttribute('id', 'card' + id);
        document.getElementById('card').appendChild(div);
    };
    Lottery.prototype.addEventOnBox = function () {
        var box = document.getElementById('box');
        var btn = document.createElement('button');
        btn.innerHTML = '点击抽取';
        btn.setAttribute('id', 'btn-lottery');
        btn.setAttribute('class', 'btn');
        btn.addEventListener('click', this.btnClickHandle.bind(this));
        box.appendChild(btn);
    };
    Lottery.prototype.addBoxEvent = function (id) {
        var _this = this;
        var dom = document.getElementById('card' + id);
        var show = document.getElementById('shows');
        dom.addEventListener('click', function () {
            // dom.style.transform = 'rotate(360deg)'
            var isShow = show.style.display !== 'none';
            var top = dom.style.top;
            console.log(show.style.display);
            if (isShow)
                return;
            show.style.display = 'block';
            dom.style.transition = 'width 1s, height 1s, top 1s, color 2s';
            dom.style.width = '300px';
            dom.style.height = '400px';
            dom.style.top = '150px';
            dom.style.zIndex = '999';
            dom.style.backgroundColor = '#fff';
            _this.rotateCard(dom, id);
            show.addEventListener('click', function () {
                show.style.display = 'none';
                dom.style.height = '100px';
                dom.style.width = '75px';
                dom.style.top = top;
                dom.style.zIndex = '1';
                dom.style.color = '#fff';
                dom.style.fontSize = '12px';
                dom.style.backgroundColor = '#000';
            });
        });
    };
    Lottery.prototype.drawCardContent = function (id) {
        var res = this.idMapResult[id] || {};
        if (Object.keys(res).length === 0)
            return;
        var container = document.getElementById('card' + id);
        var el = "\n      <div class=\"opcity\">\n        <div>" + res.name + "</div>\n        <div>" + res.type + "</div>\n        <div>" + res.word + "</div>\n        <div>" + res.message + "</div>\n      </div>\n    ";
        container.innerHTML = el;
    };
    Lottery.prototype.rotateCard = function (dom, id) {
        var _this = this;
        var i = 0;
        this.timerList['rotate' + id] = setInterval(function () {
            if (i >= 720) {
                clearInterval(_this.timerList['rotate' + id]);
                _this.drawCardContent(id);
            }
            dom.style.transform = 'rotateY(' + i + 'deg)';
            i += 16;
        }, 15);
    };
    Lottery.prototype.getResult = function () {
        var res = localStorage.getItem(this.timeStorageName);
        var rest = localStorage.getItem('_rest_gc_list');
        if (res) {
            this.result = JSON.parse(res);
            return;
        }
        var gifts = [];
        if (rest)
            gifts = JSON.parse(rest);
        else
            gifts = this.GIFTLIST;
        for (var i = 0; i < this.GET_COUNT; i++) {
            var values = this.random(gifts.length);
            console.log(values);
            this.result.push(gifts[values]);
            gifts.splice(values, 1);
        }
        localStorage.setItem(this.timeStorageName, JSON.stringify(this.result));
        !rest && localStorage.setItem('_rest_gc_list', JSON.stringify(gifts));
    };
    Lottery.prototype.btnClickHandle = function (e) {
        document.getElementById('card').innerHTML = '';
        this.getResult();
        for (var i = 0; i < this.GET_COUNT; i++) {
            var location_1 = this.scaleContainerLocation(i);
            this.drawCard(this.BOX_LOCATION.x, this.BOX_LOCATION.y, location_1.id);
            this.move(__assign(__assign({}, this.BOX_LOCATION), { id: location_1.id }), 2, location_1);
            this.addBoxEvent(location_1.id);
            this.idMapResult[location_1.id] = this.result[i];
        }
    };
    Lottery.prototype.move = function (card, time, target) {
        var _this = this;
        if (target === void 0) { target = this.BOX_LOCATION; }
        // 匀速
        // const disX = (card.x - this.BOX_LOCATION.x) / time / 10
        // const disY = (card.y - this.BOX_LOCATION.y) / time / 10
        // 变速
        var i = 0, j = 0, count = time * 10, slow = Math.floor(count / 2), fast = count - slow;
        var speedX = Math.ceil(card.x - target.x);
        var speedY = Math.ceil(card.y - target.y);
        var slowDisX = Math.ceil(speedX * 0.35);
        var slowStepX = slowDisX / slow;
        var fastDisX = speedX - slowDisX;
        var fastStepX = fastDisX / fast;
        var slowDisY = Math.ceil(speedY * 0.35);
        var slowStepY = slowDisY / slow;
        var fastDisY = speedY - slowDisY;
        var fastStepY = fastDisY / fast;
        var cd = document.getElementById('card' + card.id);
        this.timerList[card.id] = setInterval(function () {
            if (slow < 0) {
                clearInterval(_this.timerList[card.id]);
                var lenX_1 = card.x - slowDisX;
                var lenY_1 = card.y - slowDisY;
                // console.log(lenX, lenY)
                _this.timerFastList[card.id] = setInterval(function () {
                    if (fast < 0) {
                        clearInterval(_this.timerFastList[card.id]);
                        // this.addEventOnBox()
                    }
                    _this.moveFunc(cd, lenX_1 - j * fastStepX, lenY_1 - j * fastStepY);
                    j++;
                    fast--;
                }, 20);
            }
            // if (count < 0) clearInterval(this.timerList[card.id])
            _this.moveFunc(cd, card.x - slowStepX * i, card.y - slowStepY * i);
            slow--;
            i++;
            // this.moveFunc(cd, card.x - disX * i, card.y - disY * i)
            // count --
            // i ++
        }, 20);
    };
    Lottery.prototype.moveFunc = function (node, x, y) {
        node.style.left = x + 'px';
        node.style.top = y + 'px';
    };
    Lottery.prototype._getBoxLocation = function () {
        var box = document.getElementById('box');
        return {
            x: box.offsetLeft + (this.BOX.width / 2),
            y: box.offsetTop + (this.BOX.height / 4)
        };
    };
    Lottery.prototype._getMaxCount = function () {
        return Math.floor((document.body.clientWidth - 400) / (this.CARD.width + 20));
    };
    Lottery.prototype.scaleContainerLocation = function (num) {
        var left = 200 + (this.CARD.width + 20) * num;
        var top = 100 + Math.floor(num / this._getMaxCount()) * (this.CARD.height + 20);
        return {
            x: left,
            y: top,
            id: (Date.now() + num).toString()
        };
    };
    // const storage:string = localStorage.getItem('gc_storage_cardlist')
    // let res:Array<resultType>
    // if (storage) {
    //   res = JSON.parse(storage)
    // } else {
    // }
    Lottery.prototype.randomPush = function (max, count) {
        if (count === void 0) { count = 1; }
        var arr = [];
        if (count > max)
            count = max;
        while (count > 0) {
            var val = this.random(max);
            if (arr.indexOf(val) === -1) {
                arr.push(val);
                count--;
            }
        }
        if (count === 1)
            return arr[0];
        return arr;
    };
    Lottery.prototype.random = function (max) {
        return Math.floor(Math.random() * max);
    };
    Lottery.prototype._delay = function (fn, time) {
        setTimeout(function () {
            fn();
        }, time);
    };
    return Lottery;
}());
